package tech.aistar.corejava.day09.string;

/**
 * @Author:         lya
 * @Description:   本类的作用描述:可变的字符串,线程不安全的,拼接性能最高
 * @param
 * @return:
 * @CreateDate:    2019/7/25 22:04
 * @Version:       1.0
*/
public class StringBuilderDemo {
    public static void main(String[] args) {
        //利用string对象来构建一个StringBuilder对象
        //类型的转换
        //java.lang.StringBuilder提供的StringBuilder(String str);
        //作用:将字符串类型转成StringBuilder类型

        //为什么转换?
        //里面是没有"串池"的概念的!!!
        StringBuilder builder = new StringBuilder("xyz");

        //获取长度
        System.out.println(builder.length());
        System.out.println(builder);

        //字符串的拼接 - 由于它是可变的字符串
        builder.append("abc");//对原来的"xyz"对象进行直接的拼接操作
        System.out.println(builder);//xyzabc

        //不一样的地方 - 容量
        System.out.println(builder.capacity());//19

        //默认的容量是16
        System.out.println(new StringBuilder().capacity());//16

        //增删
        StringBuilder bs = new StringBuilder("java");

        //删除指定下标处的值,返回一个StringBuilder - 被删除之后的值
        StringBuilder delStr = bs.deleteCharAt(2);

        System.out.println("delStr = " + delStr);//jaa

        System.out.println(bs);

        //删除 StringBuilder delete(int start,int end);//[start,end)

        System.out.println(new StringBuilder("abcdef").delete(1,3));//adef

        //插入字符
        StringBuilder bu = new StringBuilder("pthon");

        System.out.println(bu.insert(1,"y"));//python

        //字符串反转
        StringBuilder binary = new StringBuilder("0101");

        System.out.println(binary.reverse());//1010

        //如何将Stringbuilder转换成String类型

        String bt = binary.toString();

        System.out.println(bt);
    }
}
